﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;
using System.Collections;

namespace Yuanlin.Business
{
    public class ProductCategoryCrumb : Literal
    {
        private Queue crumbs = new Queue();
        private HtmlAnchor anchor = null;
        private int productCategoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProductCategoryId);

        protected override void CreateChildControls()
        {
            ProductCategory c1 = BusinessManager.GetProductCategory(productCategoryId);
            if (c1 == null)
                return;

            string urlStr = SiteUrls.Instance().GetProductCategoryListUrl();
            if (c1.CategoryType.Value == 1)
            {
                urlStr = SiteUrls.Instance().GetApplicationCategoryListUrl();
            }
            crumbs.Enqueue(GetAnchor("花木分类列表首页", urlStr));

            crumbs.Enqueue(GetAnchor(c1.Title.Value, SiteUrls.Instance().GetProductSpecificationListUrl(c1.EntityId.Value)));
        }

        protected Control GetAnchor(string innerText, string href)
        {
            anchor = new HtmlAnchor();
            anchor.InnerHtml = innerText;
            anchor.Attributes["class"] = "crumbAnchor";
            anchor.HRef = href;

            return anchor;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("类别导航:&nbsp;&nbsp;");

            while (crumbs.Count > 0)
            {
                HtmlAnchor a = (HtmlAnchor)crumbs.Dequeue();

                a.Title = a.InnerHtml;

                if ((crumbs.Count > 0) && (a.InnerHtml.Length > 30))
                {
                    a.InnerHtml = a.InnerHtml.Substring(0, 30) + "...";
                }
                a.RenderControl(writer);

                if (crumbs.Count > 0)
                {
                    writer.Write(" &#187; ");
                }
            }

        }

    }
}
